package com.zsk.daily.accumulation.web.model.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用响应类
 *
 * @author chenwei
 */
@Data
public class ResponseMsg<T> implements Serializable {

    private static final long serialVersionUID = -2316583794584793414L;

    /**
     * 成功
     */
    private boolean success;

    /**
     * 返回代码
     */
    private String returnCode;

    /**
     * 返回味精
     */
    private String returnMsg;

    /**
     * 数据
     */
    private T data;

    public ResponseMsg() {
        this.success = true;
        this.returnCode = "200";
        this.returnMsg = "";
    }

    public ResponseMsg(boolean success, String returnCode, String returnMsg) {
        this.success = success;
        this.returnCode = returnCode;
        this.returnMsg = returnMsg;
    }

    public static <T> ResponseMsg<T> buildSuccessInstance() {
        return buildSuccessInstance(null);
    }

    public static <T> ResponseMsg<T> buildSuccessInstance(T data) {
        ResponseMsg<T> responseVo = new ResponseMsg();
        responseVo.setData(data);
        responseVo.setReturnCode("200");
        responseVo.setReturnMsg("success");
        responseVo.setSuccess(true);
        return responseVo;
    }

    public static <T> ResponseMsg<T> buildFailInstance() {
        return new ResponseMsg(false, "500", "fail");
    }

    public static <T> ResponseMsg<T> buildFailInstance(String errorCode, String msg) {
        return new ResponseMsg(false, errorCode, msg);
    }

    public static <T> ResponseMsg<T> buildFailInstance(Exception ex) {
        return new ResponseMsg(false, "500", ex.getMessage());
    }

}
